unit svm_toy;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, ShellAPI;

type
  TMainForm = class(TForm)
    MainPageControl: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    LoadGroupBox: TGroupBox;
    ScaleGroupBox: TGroupBox;
    MainStatusBar: TStatusBar;
    LoadTrainLabel: TLabel;
    LoadTrainEdit: TEdit;
    LoadTrainButton: TButton;
    ScaleTrainButton: TButton;
    TrainFileEdit: TEdit;
    Label1: TLabel;
    ScaledTrainFileEdit: TEdit;
    GridGroupBox: TGroupBox;
    GridLabel: TLabel;
    ScaledTrainFile: TEdit;
    GridButton: TButton;
    TrainGroupBox: TGroupBox;
    TrainLabel: TLabel;
    SVMTrainFileEdit: TEdit;
    TrainFileLabel: TLabel;
    ScaledTrainFileLabel: TLabel;
    c_Label: TLabel;
    C_Edit: TEdit;
    g_Label: TLabel;
    G_Edit: TEdit;
    TrainButton: TButton;
    LoadTestLabel: TLabel;
    LoadTestEdit: TEdit;
    LoadTestButton: TButton;
    ScaleTestButton: TButton;
    TestFileEdit: TEdit;
    Label2: TLabel;
    ScaledTestFileEdit: TEdit;
    TestFileLabel: TLabel;
    ScaledTestFileLabel: TLabel;
    PredictGroupBox: TGroupBox;
    TestLabel: TLabel;
    SVMTestFileEdit: TEdit;
    TestButton: TButton;
    OpenDialog1: TOpenDialog;
    OutPutMemo: TMemo;
    procedure LoadTrainButtonClick(Sender: TObject);
    procedure LoadTestButtonClick(Sender: TObject);
    procedure ScaleTrainButtonClick(Sender: TObject);
    procedure ScaleTestButtonClick(Sender: TObject);
    procedure GridButtonClick(Sender: TObject);
    procedure TrainButtonClick(Sender: TObject);
    procedure TestButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;
  TrainOfFile:TextFile;
  TestOfFile:TextFile;
  TrainFileName:string;
  TestFileName:string;
  ScaledTrainFileName:string;
  ScaledTestFileName:string;
  CurrentDir:string;

implementation

{$R *.dfm}

procedure _AddInfo(mmInfo: TMemo;S: string;var line: string);
var
  i,p:Integer;
begin
  if mmInfo.Lines.Count > 800 then
    mmInfo.Lines.Clear;

  for i := 0 to Length(S) - 1 do
    if S[i] = #13 then S[i] := ' ';
  line := line + S;

  p := Pos(#10,line);
  if p > 0 then
  begin
    mmInfo.Lines.Add(Copy(line,1,p - 1));
    line := Copy(line,p + 1,Length(line) - p);
  end;
end;

procedure CmdExecAndView(FileName: string;memo: TMemo);
var
  hReadPipe,hWritePipe: THandle;
  si: STARTUPINFO;
  lsa: SECURITY_ATTRIBUTES;
  pi: PROCESS_INFORMATION;
  cchReadBuffer: DWORD;
  ph: PChar;
  fname: PChar;
  line: string;
begin
  fname := AllocMem(1024);
  ph := AllocMem(1024);
  lsa.nLength := SizeOf(SECURITY_ATTRIBUTES);
  lsa.lpSecurityDescriptor := nil;
  lsa.bInheritHandle := True;

  if CreatePipe(hReadPipe,hWritePipe,@lsa,0) = False then
    Exit;

  FillChar(si,SizeOf(STARTUPINFO),0);
  si.cb := SizeOf(STARTUPINFO);
  si.dwFlags := (STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW);
  si.wShowWindow := SW_HIDE;
  si.hStdOutput := hWritePipe;
  si.hStdError := hWritePipe;
  StrPCopy(fname,FileName);

  if CreateProcess(nil,fname,nil,nil,True,0,nil,nil,si,pi) = False then
  begin
    FreeMem(ph);
    FreeMem(fname);
    Exit;
  end;

  CloseHandle(hWritePipe);

  while (True) do
  begin
    if not PeekNamedPipe(hReadPipe,ph,1,@cchReadBuffer,nil,nil) then Break;
    if cchReadBuffer <> 0 then
    begin
      if ReadFile(hReadPipe,ph^,512,cchReadBuffer,nil) = False then Break;
      ph[cchReadBuffer] := chr(0);
      _AddInfo(memo,ph,line);
    end
    else if (WaitForSingleObject(pi.hProcess,0) = WAIT_OBJECT_0) then Break;
    Application.ProcessMessages;
    Sleep(200);
  end;

  ph[cchReadBuffer] := chr(0);
  _AddInfo(memo,ph,line);
  CloseHandle(hReadPipe);
  CloseHandle(pi.hThread);
  CloseHandle(pi.hProcess);
  FreeMem(ph);
  FreeMem(fname);

end;

procedure TMainForm.LoadTrainButtonClick(Sender: TObject);
begin
  OpenDialog1.Filter := 'Text File';
  OpenDialog1.InitialDir := '.\data';
  if OpenDialog1.Execute then
  begin
    AssignFile(TrainOfFile,OpenDialog1.FileName);
    LoadTrainEdit.Text := OpenDialog1.FileName;
    TrainFileName := ExtractFileName(OpenDialog1.FileName);
    TrainFileEdit.Text := TrainFileName;
    ScaledTrainFileName := TrainFileName + '.scale';
    ScaledTrainFileEdit.Text := ScaledTrainFileName;
    ScaledTrainFile.Text := ScaledTrainFileName;
    SVMTrainFileEdit.Text := ScaledTrainFileName;
  end;

end;

procedure TMainForm.LoadTestButtonClick(Sender: TObject);
begin
  OpenDialog1.Filter := 'Text File';
  OpenDialog1.InitialDir := '.\data';
  if OpenDialog1.Execute then
  begin
    AssignFile(TestOfFile,OpenDialog1.FileName);
    LoadTestEdit.Text := OpenDialog1.FileName;
    TestFileName := ExtractFileName(OpenDialog1.FileName);
    TestFileEdit.Text := TestFileName;
    ScaledTestFileName := TestFileName + '.scale';
    ScaledTestFileEdit.Text := ScaledTestFileName;
    SVMTestFileEdit.Text := ScaledTestFileName;
  end;
end;

procedure TMainForm.ScaleTestButtonClick(Sender: TObject);
begin
  SetCurrentDir(CurrentDir);
//  ShellExecute(Handle,'open',PChar('cmd.exe'),
//               PChar('/k svm-scale -r data\scale data\' + TestFileEdit.Text +
//                     '>data\' + ScaledTestFileEdit.Text),nil,SW_HIDE);
  CmdExecAndView('cmd.exe /k svm-scale -r data\scale data\' + TestFileEdit.Text +
                 '>data\' + ScaledTestFileEdit.Text,OutPutMemo);
end;

procedure TMainForm.ScaleTrainButtonClick(Sender: TObject);
begin
  SetCurrentDir(CurrentDir);
//  ShellExecute(Handle,'open',PChar('cmd.exe'),
//               PChar('/k svm-scale -s data\scale data\' + TrainFileEdit.Text +
//                     '>data\' + ScaledTrainFileEdit.Text),nil,SW_HIDE);
//    ShellExecute(Handle,'open',PChar('cmd.exe'),PChar('/k svm-scale -s data\scale data\a1a>data\a1a.scale'),nil,SW_SHOWNORMAL);
//  CmdExecAndView('cmd.exe /k svm-scale -s data\scale data\' + TrainFileEdit.Text +
//                  '>data\' + ScaledTrainFileEdit.Text,OutPutMemo);
  CmdExecAndView('ping www.sina.com',OutPutMemo);
end;

procedure TMainForm.TestButtonClick(Sender: TObject);
begin
  SetCurrentDir(CurrentDir);
//   ShellExecute(Handle,'open',PChar('cmd.exe'),
//                PChar('/k svm-predict data\' + SVMTestFileEdit.Text + ' data\' +
//                      ScaledTrainFileEdit.Text + '.model' + ' data\' +
//                      SVMTestFileEdit.Text + '.out'),nil,SW_HIDE);
  CmdExecAndView('cmd.exe /k svm-predict data\' + SVMTestFileEdit.Text + ' data\' +
                 ScaledTrainFileEdit.Text + '.model' + ' data\' +
                 SVMTestFileEdit.Text + '.out',OutPutMemo);
end;

procedure TMainForm.TrainButtonClick(Sender: TObject);
begin
  SetCurrentDir(CurrentDir);
//   ShellExecute(Handle,'open',PChar('cmd.exe'),
//                PChar('/k svm-train -c ' + C_Edit.Text + ' -g ' + G_Edit.Text +
//                      ' data\' + SVMTrainFileEdit.Text),nil,SW_HIDE);
  CmdExecAndView('cmd.exe /k svm-train -c ' + C_Edit.Text + ' -g ' + G_Edit.Text +
                 ' data\' + SVMTrainFileEdit.Text,OutPutMemo);
end;

procedure TMainForm.GridButtonClick(Sender: TObject);
begin
  SetCurrentDir(CurrentDir);
//  ShellExecute(Handle,'open',PChar('cmd.exe'),
//               PChar('/k python grid.py data\' + ScaledTrainFile.Text),
//               nil,SW_SHOWNORMAL);
  CmdExecAndView('cmd.exe /k python grid.py data\' + ScaledTrainFile.Text,OutPutMemo);
end;

begin
  CurrentDir := GetCurrentDir;

end.
